package canss.webservice.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import canss.webservice.model.ModuleDesc;
import canss.webservice.service.PortalIntraWebServiceControl;
import canss.webservice.service.PortalIntraWebServiceControlBean;

public class svtOPMIDDesc extends javax.servlet.http.HttpServlet implements
		javax.servlet.Servlet
{
	private static final long serialVersionUID = -5876819912803898711L;

	public svtOPMIDDesc()
	{
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		doPost(request, response);
	}

	PortalIntraWebServiceControl control;
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		String _notxml = request.getParameter("_notxml");
		
		control = (control == null) ? new PortalIntraWebServiceControlBean() : control;
		if (_notxml != null)
		{// in html
		} else
		{// in xml
			out.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
			weblogic.application.utils.XMLWriter writer = new weblogic.application.utils.XMLWriter(out);

			writer.addElement("canss");
			{
				ModuleDesc[] moduledesclist = control.getEntireModuleDesc();
				if(moduledesclist!=null && moduledesclist.length>0)
				{
					for(ModuleDesc moduledesc : moduledesclist)
					{
						writer.addElement("canssModule_module");
						{
							writer.addElement("canssModule_mid", moduledesc.getTBLMID_MID());
							writer.addElement("canssModule_name", moduledesc.getNAME());
							writer.addElement("canssPerson_name", moduledesc.getTBLPID_PID());

							writer.addElement("canssModule_commands");
							String[] strs=moduledesc.getCOMMANDS().split(",");
							if(strs.length>0)
							{
								for(int i=0; i<strs.length; i++)
									writer.addElement("canssModule_command", strs[i].trim());
							}
							writer.closeElement();
							
							writer.addElement("canssModule_netaddr", moduledesc.getNETADDR());
							{
								final long mintimedisconnected = 20000; //same to 'getAjaxGetModuleStatusByMID'
								if(moduledesc.getTIMESTAMP() + mintimedisconnected < System.currentTimeMillis())
									moduledesc.setMIDSTATUS("disconnected");
								writer.addElement("canssModule_status_2", moduledesc.getMIDSTATUS());
							}
							StringBuffer code = new StringBuffer();
							code.append("document.getElementById('div_canssModule_status_2_{$v_mid}').innerHTML=document.getElementById('overlay3').innerHTML;");
							code.append("var hobj=getHTTPObject();if(hobj==null) {document.getElementById('div_canssModule_status_2_{$v_mid}').innerHTML='"+moduledesc.getMIDSTATUS()+"';return;}");
							code.append("var param='_mid={$v_mid}';");
							code.append("hobj.open('POST','getAjaxGetModuleStatusByMID',true);hobj.setRequestHeader('Content-type','application/x-www-form-urlencoded');hobj.setRequestHeader('Content-length',param.length);hobj.setRequestHeader('Connection','close');hobj.onreadystatechange=function()");
							code.append("{if(hobj.readyState==4 && hobj.status==200)document.getElementById('div_canssModule_status_2_{$v_mid}').innerHTML=hobj.responseText;};");
							code.append("hobj.send(param);");
							writer.addElement("canssModule_status_2_js",code.toString().replace("{$v_mid}", moduledesc.getTBLMID_MID()).replace("&", "&amp;"));
						}
						writer.closeElement();
					}
				}
			}
			writer.closeElement();
			
			writer.finish();
		}
	}
}